"""   Copyright 2011 Bart Enkelaar

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""

from tweetcleaner import TextExtractor
import unittest



class TestLineCleaning(unittest.TestCase):
    def setUp(self):
        self.extractor = TextExtractor("geektrip")
        
    def testRemoveUrl(self):
        input = "Neat! http://www.nasa.gov/geektrip/with/loads/off http://fun"
        self.assertEquals("Neat!", self.extractor._remove_uris(input))
        
    def testRemoveUrlNoHttp(self):
        input = "Neat! www.nasa.gov/geektrip/with/loads/off ftp://fun/goodies/"
        self.assertEquals("Neat!", self.extractor._remove_uris(input))
    
    def testDoubleHashTagStrip(self):
        input = "Waaaaaaaazzzzzuuuuuuuuuup! #geektrip #WONDERFUL"
        self.assertEquals("Waaaaaaaazzzzzuuuuuuuuuup!", self.extractor._remove_tags(input))

    def testStripStartingHash(self):
        input = "#geektrip I've still no idea of what to hack!"
        self.assertEquals("I've still no idea of what to hack!", self.extractor._remove_tags(input))

    def testStripHashFromTagAfter(self):
        input = "I'm having a great time at #geektrip"
        self.assertEquals(input.replace("#", ""), self.extractor._remove_tags(input))

    def testLeaveAndStrip(self):
        input = "The #geektrip is happening! #awesomesauce"
        self.assertEquals("The geektrip is happening!", self.extractor._remove_tags(input))

    def testRemoveStartingAdNotation(self):
        input = "@sanderk Geek trip is absolutely awesome!"
        self.assertEquals("Geek trip is absolutely awesome!", self.extractor._remove_at_names(input))

    def testRemoveAdAdvanced(self):
        input = "RT @sanderk @geektrip Me and @Angry_B8 are working with Arduinos!"
        self.assertEquals("Me and Angry_B8 are working with Arduinos!", self.extractor._remove_at_names(input))
        
    def testRemoveHashAndSplit(self):
        input = "We're on a #geektrip! What will @erik and I do? We don't know. #theworldisawesome"
        self.assertEquals(["We're on a geektrip!", "What will erik and I do?",
                           "We don't know"],
                          self.extractor.extract_english(input))


class TestSentenceSplitting(unittest.TestCase):
    def setUp(self):
        self.extractor = TextExtractor("geektrip")
    
    def testExtractBracketedSentence(self):
        input = "When I walk alone, (I like to do that sometimes) I think hard"
        self.assertEquals(["I like to do that sometimes",
                           "When I walk alone,  I think hard"], 
                          self.extractor._split_sentences(input))
    
    def testExtractBracketedSentences(self):
        input = "When I walk alone, (I like to do that sometimes (I'm weird like that)) I think hard"
        self.assertEquals(["I'm weird like that",
                           "I like to do that sometimes",
                           "When I walk alone,  I think hard"
                          ], 
                          self.extractor._split_sentences(input))
    
    def testRemoveSmiley(self):
        input = "When I walk alone, :-( I think happy thoughts :D"
        self.assertEquals("When I walk alone, I think happy thoughts",
                          self.extractor._remove_smileys(input).replace("  ", " ").strip())
    
    def testRemoveSmileys(self):
        input = "B-] BD 80) :-[ :-o ;) ^_^ ^_o 0_O"
        self.assertEquals("", self.extractor._remove_smileys(input).strip())
    
    def testDontRemoveBooh(self):
        input = "Booh Boo"
        self.assertEquals("Booh Boo",
                          self.extractor._remove_smileys(input))
    
    def testSplitLongSentenceOnComma(self):
        self.extractor.line_limit = 20
        input = ["When I walk alone, I always think hard"]
        self.assertEquals(["When I walk alone"," I always think hard"],
                          self.extractor._control_length(input))
    
    def testSplitLongSentenceOnCenterCommaOnly(self):
        self.extractor.line_limit = 30
        input = ["You know, when I'm alone, I always think hard"]
        self.assertEquals(["You know, when I'm alone"," I always think hard"],
                          self.extractor._control_length(input))
        
    def testSplitLongSentenceOnSpace(self):
        self.extractor.line_limit = 20
        input = ["When I walk alone I will think hard"]
        self.assertEquals(["When I walk alone"," I will think hard"],
                          self.extractor._control_length(input))
        
    def testDontSplitShortSentence(self):
        self.extractor.line_limit = 20
        input = ["A, B, C, D"]
        self.assertEquals(["A, B, C, D"], self.extractor._control_length(input))
        
    def testSplitOnDot(self):
        input = "There is a geek. He's on a trip!"
        self.assertEquals(["There is a geek", "He's on a trip!"],
                          self.extractor._split_sentences(input))
        
    def testSplitOnQuestionMark(self):
        input = "Is there a geek? There is on #geektrip"
        self.assertEquals(["Is there a geek?", "There is on #geektrip"],
                          self.extractor._split_sentences(input))
        

        
if __name__ == "__main__":
    unittest.main()